package fr.xelaprod.open.source.android.rss;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import fr.xelaprod.open.source.android.rss.db.MyNewsHandleBDD;
import fr.xelaprod.open.source.android.rss.graphicsElements.DoubleChoicePopUpInsertNews;
import fr.xelaprod.open.source.android.rss.graphicsElements.SimplePopUp;
import fr.xelaprod.open.source.android.rss.object.Item;
import fr.xelaprod.open.source.android.rss.object.ReadRSS;
import android.app.Activity;
import android.content.Context;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class LesNews extends Activity {

	private ReadRSS readRSS;
	private EventsList listAdapter;
	public static final String RSSAdress = "http://twitter.com/statuses/user_timeline/57732899.rss";
	private MyNewsHandleBDD myNews;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		readRSS = new ReadRSS(RSSAdress);
		myNews = new MyNewsHandleBDD(this);

	}

	public boolean checkUserHasAConnection() {
		ConnectivityManager connec = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		State mobileNetwork = connec.getNetworkInfo(0).getState();
		State wirelessNetwork = connec.getNetworkInfo(1).getState();
		if (mobileNetwork == NetworkInfo.State.CONNECTED
				|| mobileNetwork == NetworkInfo.State.CONNECTING)
			return true;
		else if (wirelessNetwork == NetworkInfo.State.CONNECTED
				|| wirelessNetwork == NetworkInfo.State.CONNECTING)
			return true;
		else {
			new SimplePopUp(this, "NetWork problem",
					"Your network is not available, please turn the mobile/wireless network on");
			return false;
		}
	}

	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.menu, menu);
		return true;
	}

	public void showNews(int limitBegin, int limitEnd) {
		if (checkUserHasAConnection()) {
			List<Item> listOfNews = readRSS.parse(limitBegin, limitEnd);
			final List<String> mStrings = new ArrayList<String>();

			for (int i = 0; i < listOfNews.size(); i++) {
				mStrings.add(listOfNews.get(i).getDescription() + "\n"
						+ listOfNews.get(i).getPubDate());

			}
			listAdapter = new EventsList(this, R.layout.rowlayoutitems,
					R.id.labelNews, mStrings);
			ListView listView = (ListView) findViewById(R.id.listMm);
			listView.setAdapter(listAdapter);
			listView.setTextFilterEnabled(true);

			listView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					final int arg = arg2;
					new DoubleChoicePopUpInsertNews(LesNews.this, "Save the news",
							"Would you save this news ?", "Save", "Resume",
							myNews, mStrings.get(arg));
				}

			});
		}

	}

	public void showMyNews() {
		myNews.open();
		final List<String> mStrings = new ArrayList<String>();
		final Map<Integer, String> mapOfNews = myNews.getItems();
		int key;
		for (Iterator<Integer> i = mapOfNews.keySet().iterator() ; i.hasNext() ; ){
		    key = (Integer) i.next();
		    mStrings.add(key+"/ "+mapOfNews.get(key));
		}
		
		listAdapter = new EventsList(this, R.layout.rowlayoutitems,
				R.id.labelNews, mStrings);
		ListView listView = (ListView) findViewById(R.id.listMm);
		listView.setAdapter(listAdapter);
		listView.setTextFilterEnabled(true);
		
		/*listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				final int arg = arg2;
				String id = mStrings.get(arg).substring(0, mStrings.get(arg).indexOf("/"));
				new DoubleChoicePopUpDeleteNews(LesNews.this, "Delete the news",
						"Would you delete this news ?", "Delete", "Resume",
						myNews,Integer.valueOf(id));
			}
		});*/
		myNews.close();
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.news: {
			setContentView(R.layout.news);
			showNews(0, 20);
			return true;
		}
		case R.id.mynews: {
			setContentView(R.layout.news);
			showMyNews();
			return true;
		}
		case R.id.home: {
			setContentView(R.layout.main);
			return true;
		}
		}
		return false;
	}
}